package com.wmh.baseservice.operationaid.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.operationaid.entity.ProductTrafficSource;
import com.wmh.baseservice.operationaid.pojo.vo.productTrafficSource.ProductTrafficSourcePageReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.productTrafficSource.ProductTrafficSourceReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.productTrafficSource.ProductTrafficSourceRobotCommitReqVO;
import com.wmh.baseservice.operationaid.service.ProductTrafficSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author dongaolong
 * @date 2023/7/22
 **/
@Validated
@RestController
@RequestMapping("product/trafficSource")
@Api(tags = "流量来源")
@Slf4j
public class ProductTrafficSourceController {

    @Resource
    private ProductTrafficSourceService productTrafficSourceService;


    @ApiOperation(value = "机器提交数据")
    @PostMapping("/robot/public/CommitData")
    public String robotCommitData(@Validated ProductTrafficSourceRobotCommitReqVO reqVO) {
        productTrafficSourceService.robotCommitData(reqVO);
        return success();
    }

    @ApiOperation("获取店铺名称")
    @PostMapping("/storeNameList")
    public String getStoreNameList() {
        return success(productTrafficSourceService.getStoreNameList());
    }

    @ApiOperation("获取数据周期")
    @PostMapping("/dataCycleList")
    public String getDataCycleList() {
        return success(productTrafficSourceService.getDataCycleList());
    }

    @ApiOperation("获取流量来源")
    @PostMapping("/trafficsSourceList")
    public String getTrafficsSourceList(@Validated ProductTrafficSourceReqVO reqVO) {
        PageResult<ProductTrafficSource> pageResult = new PageUtil<ProductTrafficSource>(reqVO).startPage((page, wrapper) -> {

            List<String> trafficsSourceList = reqVO.getTrafficsSourceNameListJson() != null ?
                    JSON.parseArray(reqVO.getTrafficsSourceNameListJson(), String.class) : null;

            wrapper.eqIfPresent(ProductTrafficSource.COL_PRODUCT_ID, reqVO.getProductId());
            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), ProductTrafficSource.COL_STORE_NAME, reqVO.getStoreName());
            wrapper.inIfPresent(ProductTrafficSource.COL_TRAFFICS_SOURCE, trafficsSourceList);
            wrapper.eq(StrUtil.isNotBlank(reqVO.getDataCycle()), ProductTrafficSource.COL_DATA_CYCLE, reqVO.getDataCycle());
            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), ProductTrafficSource.COL_STORE_NAME, reqVO.getStoreName());
            productTrafficSourceService.page(page, wrapper);
        });
        return success(pageResult);
    }

    @ApiOperation("分页")
    @PostMapping("page")
    public String page(@Validated @RequestBody ProductTrafficSourcePageReqVO reqVO) {

        PageResult<ProductTrafficSource> pageResult = new PageUtil<>(reqVO, ProductTrafficSource.class).startPage((page, wrapper) -> {
            LambdaQueryWrapper<ProductTrafficSource> l = wrapper.lambda();
            wrapper.eqIfPresent(ProductTrafficSource.COL_PRODUCT_ID, reqVO.getProductId());
            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), ProductTrafficSource.COL_STORE_NAME, reqVO.getStoreName());
            wrapper.inIfPresent(ProductTrafficSource.COL_TRAFFICS_SOURCE, reqVO.getTrafficsSourceNameList());
            wrapper.eq(StrUtil.isNotBlank(reqVO.getDataCycle()), ProductTrafficSource.COL_DATA_CYCLE, reqVO.getDataCycle());
            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), ProductTrafficSource.COL_STORE_NAME, reqVO.getStoreName());
            productTrafficSourceService.page(page, wrapper);
        });
        return success(pageResult);
    }

    @ApiOperation("商品流量来源下载为 Excel 表格文件")
    @PostMapping("/download")
    public void downloadExcel(HttpServletResponse response, @Validated ProductTrafficSourceReqVO reqVO) {
        productTrafficSourceService.writeExcel(response, reqVO);
    }
}
